package com.bjc.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

/**
 * @program: ssmpractice
 * @description: 事务工具类
 * @author: bjc
 * @create: 2020-08-31 21:56
 */
@Component
public class TransactionUtils {

    @Autowired
    private DataSourceTransactionManager transactionManager;


    //开启事务
    public TransactionStatus begin(){
        TransactionDefinition arg0 = new DefaultTransactionDefinition();
        TransactionStatus transaction = transactionManager.getTransaction(arg0);
        return transaction;
    }

    //提交事务
    public void commit(TransactionStatus trans){
        transactionManager.commit(trans);
    }

    //事务回滚
    public void rollback(TransactionStatus trans){
        transactionManager.rollback(trans);
    }
}
